package org.study.auth.shiro.chapter2

import org.apache.shiro.SecurityUtils
import org.apache.shiro.authc.AuthenticationException
import org.apache.shiro.authc.UsernamePasswordToken
import org.apache.shiro.config.IniSecurityManagerFactory
import org.junit.Assert
import org.junit.Test

class LoginTest {

    @Test
    fun testLogin() {
        val factory = IniSecurityManagerFactory("classpath:shiro.ini")
        val securityManager = factory.instance
        SecurityUtils.setSecurityManager(securityManager)

        val subject = SecurityUtils.getSubject()
        val usernamePasswordToken = UsernamePasswordToken("ldd", "123")
        // 尝试登录
        try {
            subject.login(usernamePasswordToken)
        } catch (ex: AuthenticationException) {

        }
        // 断言是否登录成功
        Assert.assertEquals(true, subject.isAuthenticated)
        // 退出
        subject.logout()
    }
}